package com.missmess.opengldemo.ui;

import android.opengl.GLU;
import android.os.Bundle;

import com.missmess.opengldemo.GlBaseActivity;
import com.missmess.opengldemo.model.Sphere;
import com.missmess.opengldemo.util.BufferUtils;

import java.nio.FloatBuffer;

import javax.microedition.khronos.opengles.GL10;

public class Lighting extends GlBaseActivity {
    Sphere sphere = new Sphere();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    public void initScene(GL10 gl) {
        float[] amb = {1.0f, 1.0f, 1.0f, 1.0f,};
        float[] diff = {1.0f, 1.0f, 1.0f, 1.0f,};
        float[] spec = {1.0f, 1.0f, 1.0f, 1.0f,};
        float[] pos = {0.0f, 5.0f, 5.0f, 1.0f,};
        float[] spot_dir = {0.0f, -1.0f, 0.0f,};
        gl.glEnable(GL10.GL_DEPTH_TEST);
        gl.glEnable(GL10.GL_CULL_FACE);

        gl.glEnable(GL10.GL_LIGHTING);
        gl.glEnable(GL10.GL_LIGHT0);

        FloatBuffer ambBuf = BufferUtils.float2Buffer(amb);
        FloatBuffer diffBuf = BufferUtils.float2Buffer(diff);
        FloatBuffer specBuf = BufferUtils.float2Buffer(spec);
        FloatBuffer posBuf = BufferUtils.float2Buffer(pos);
        FloatBuffer spot_dirBuf = BufferUtils.float2Buffer(spot_dir);

        gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_AMBIENT, ambBuf);
        gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_DIFFUSE, diffBuf);
        gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_SPECULAR, specBuf);
        gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_POSITION, posBuf);
        gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_SPOT_DIRECTION, spot_dirBuf);
        gl.glLightf(GL10.GL_LIGHT0, GL10.GL_SPOT_EXPONENT, 0.0f);
//        gl.glLightf(GL10.GL_LIGHT0, GL10.GL_SPOT_CUTOFF, 45.0f);

        gl.glLoadIdentity();
        GLU.gluLookAt(gl,
                0.0f, 4.0f, 4.0f,
                0.0f, 0.0f, 0.0f,
                0.0f, 1.0f, 0.0f);
    }

    public void drawScene(GL10 gl) {
        super.drawScene(gl);

		float[] mat_amb = {0.2f * 0.4f, 0.2f * 0.4f, 0.2f * 1.0f, 1.0f,};
        float[] mat_diff = {0.4f, 0.4f, 1.0f, 1.0f,};
		float[] mat_spec = {1.0f, 1.0f, 1.0f, 1.0f,};


        FloatBuffer mat_ambBuf = BufferUtils.float2Buffer(mat_amb);
        FloatBuffer mat_diffBuf = BufferUtils.float2Buffer(mat_diff);
        FloatBuffer mat_specBuf = BufferUtils.float2Buffer(mat_spec);

    	gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_AMBIENT, mat_ambBuf);
    	gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_DIFFUSE, mat_diffBuf);
    	gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_SPECULAR, mat_specBuf);
    	gl.glMaterialf(GL10.GL_FRONT_AND_BACK, GL10.GL_SHININESS, 64.0f);

        initScene(gl);
        sphere.draw(gl);
    }
}
